// components/to-home/toHome.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  options: {
    addGlobalClass: true
  },

  /**
   * 组件的初始数据
   */
  data: {
    flags: false,
    x: 0,
    y: 0,
    nx: '',
    ny: '',
    dx: '',
    dy: '',
    xPum: '',
    yPum: '',
    top: '45vh',
    left: '4.15vh'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    gohome () {
      wx.reLaunch({url: '/pages/home-page/index/index'})
    },
    down (event) {
      this.data.flags = true
      var touch
      if (event.touches) {
        touch = event.touches[0]
      } else {
        touch = event
      }
      var queryHome = this.createSelectorQuery()
      queryHome.select('.homeview').boundingClientRect()
      var self = this
      queryHome.exec(function(res) {
        console.log(res, touch)
        self.data.maxW = wx.getSystemInfoSync().windowWidth - res[0].width
        self.data.maxH = wx.getSystemInfoSync().windowHeight - res[0].height
        self.data.x = touch.clientX - res[0].left
        self.data.y = touch.clientY - res[0].top
        self.data.dx = touch.clientX
        self.data.dy = touch.clientY
        self.setData({
          maxW: self.data.maxW,
          maxH: self.data.maxH,
          x: self.data.x,
          y: self.data.y,
          dx: self.data.dx,
          dy: self.data.dy
        })
      })
    },
    move(event) {
      if (this.data.flags) {
        var touch
        if (event.touches) {
          touch = event.touches[0]
        } else {
          touch = event
        }
        this.data.nx = touch.clientX - this.data.x
        this.data.ny = touch.clientY - this.data.y
        if (this.data.nx < 0) {
          this.data.nx = 0
        } else if (this.data.nx > this.data.maxW) {
          this.data.nx = this.data.maxW
        }
        if (this.data.ny < 0) {
          this.data.ny = 0
        } else if (this.data.ny >= this.data.maxH) {
          this.data.ny = this.data.maxH
        }
        this.setData({
          left: this.data.nx + 'px',
          top: this.data.ny + 'px'
        })
      }
    },
    end () {
      this.flags = false
    }
  }
})
